package com.aefan.springcloud.controller;

import com.aefan.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: jxl
 * @Date: 2021/9/17 下午4:30
 * Hystrix服务降级处理（程序异常、超时、熔断触发、线程池/信号量打满）
 */
@RestController
@Slf4j
@RequestMapping("/customer")
//@DefaultProperties 服务降级全局默认配置调用方法，方法头配置@HystrixCommand生效，方法头配置@HystrixCommand，@HystrixProperty优先
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_OK(id);
    }

    //服务降级 @HystrixCommand
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//    })
    @HystrixCommand
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }

    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        int aegs = 10/0;//抛异常
        return "我是消费者80，对方支付系统繁忙请10s后重试或者运行出错，请检查自己";
    }

    public String payment_Global_FallbackMethod(){
        return "Glpbal 异常处理信息，请稍后再试～～";
    }


}
